package com.cgsd.heyu.utils;

import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * author Administrator
 * time:22:34
 * descrition:
 */
public class StringUtils {


    /**
     * 键盘搜索
     * @param mEditText
     * @param onEditTextActionListener
     */
    public static void editTextSearchAction(EditText mEditText, OnEditTextSearchListener onEditTextActionListener) {
        mEditText.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String keyWords = mEditText.getText().toString().trim();
                onEditTextActionListener.onEditorAction(keyWords);
                return true;
            }
            return false;
        });
    }

    public interface OnEditTextSearchListener {
        void  onEditorAction(String content);
    }

    /**
     * md5加密
     *
     * @param string
     * @return
     */
    public static String str2MD5(String string) {

        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return "";
    }

}
